package com.meida.module.bus.provider.service.impl;

import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.meida.common.base.entity.EntityMap;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.mybatis.base.service.impl.BaseServiceImpl;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.utils.JsonUtils;
import com.meida.module.bus.provider.service.BusConfigService;
import com.meida.module.system.client.entity.SysConfig;
import com.meida.module.system.provider.mapper.BaseConfigMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统配置表 服务实现类
 *
 * @author flyme
 * @date 2019-06-10
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class BusConfigServiceImpl extends BaseServiceImpl<BaseConfigMapper, SysConfig> implements BusConfigService {

    public void initBaseConfig() {
        String configKey = "CONFIG";
        Map<Object, Object> configMaps = new HashMap<>(0);
        List<SysConfig> configs = list();
        if (ObjectUtils.isNotEmpty(configs)) {
            redisUtils.delByProjectName(configKey);
            configs.forEach(config -> {
                String key = config.getConfigKey();
                configMaps.put(key, config.getConfigVal());

            });
            redisUtils.setProjectMap(configKey, configMaps);
            log.info("Redis初始化完成...");
        }
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public List<SysConfig> listByType(String configType) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq(true, "configType", configType);
        return list(qw);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public SysConfig getByKey(String configKey) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq(true, "configKey", configKey);
        return getOne(qw);
    }


    @Override
    public ResultBody configSave(Map params) {
        CriteriaSave cs = new CriteriaSave(params, entityClass);
        String configKey = cs.getParams("configKey");
        if (FlymeUtils.isEmpty(configKey)) {
            return ResultBody.failed("configKey必传");
        }
        SysConfig config = getByKey(configKey);
        SysConfig newConfig = cs.getEntity(SysConfig.class);
        if (FlymeUtils.isNotEmpty(config)) {
            newConfig.setConfigId(config.getConfigId());
        }
        saveOrUpdate(newConfig);
        initBaseConfig();
        return ResultBody.ok("设置成功", true);
    }


    @Override
    public ResultBody saveCharges(Map params) {
        JSONObject object = JSON.parseObject(MapUtil.getStr(params, "params"));
        String describe = object.getString("describe");
        BigDecimal charges = object.getBigDecimal("charges");
        Integer baseline = object.getInteger("baseline");
        EntityMap map = new EntityMap();
        map.put("describe", describe);
        map.put("charges", charges);
        map.put("baseline", baseline);
        SysConfig config = getByKey("CHARGES_CONFIG");
        if (FlymeUtils.isEmpty(config)) {
            config = new SysConfig();
            config.setConfigKey("CHARGES_CONFIG");
            config.setConfigType("charges");
            config.setRemark("平台提现介绍");
        }
        config.setConfigVal(JsonUtils.beanToJson(map));
        saveOrUpdate(config);
        initBaseConfig();
        return ResultBody.ok("设置成功", true);
    }

    @Override
    public ResultBody savePartner(Map params) {
        JSONObject obj = JSON.parseObject(MapUtil.getStr(params, "params"));
        String group = obj.getString("group");
        Integer member = obj.getInteger("member");
        EntityMap map = new EntityMap();
        map.put("group", group);
        map.put("member", member);
        SysConfig config = getByKey("PARTNER_GROUP_NUM");
        if (FlymeUtils.isEmpty(config)) {
            config = new SysConfig();
            config.setConfigKey("PARTNER_GROUP_NUM");
            config.setConfigType("partner");
            config.setRemark("预售合伙人设置");
        }
        config.setConfigVal(JsonUtils.beanToJson(map));
        saveOrUpdate(config);
        initBaseConfig();
        return ResultBody.ok("设置成功", true);
    }

    @Override
    public ResultBody saveOrder(Map params) {
        JSONObject obj = JSON.parseObject(MapUtil.getStr(params, "params"));
        Integer orderInvalid = obj.getInteger("orderInvalid");
        EntityMap map = new EntityMap();
        map.put("orderInvalid", orderInvalid);
        SysConfig config = getByKey("ORDER_INVALID_CONFIG");
        if (FlymeUtils.isEmpty(config)) {
            config = new SysConfig();
            config.setConfigKey("ORDER_INVALID_CONFIG");
            config.setConfigType("order");
            config.setRemark("订单自动取消设置");
        }
        config.setConfigVal(JsonUtils.beanToJson(map));
        saveOrUpdate(config);
        initBaseConfig();
        return ResultBody.ok("设置成功", true);
    }

    @Override
    public ResultBody saveBonus(Map params) {
        JSONObject obj = JSON.parseObject(MapUtil.getStr(params, "params"));
        BigDecimal sameGroup = obj.getBigDecimal("sameGroup");
        BigDecimal differentGroup = obj.getBigDecimal("differentGroup");
        BigDecimal temporaryGroup = obj.getBigDecimal("temporaryGroup");
        BigDecimal agvGroup = obj.getBigDecimal("agvGroup");

        EntityMap map = new EntityMap();
        map.put("sameGroup", sameGroup);
        map.put("differentGroup", differentGroup);
        map.put("temporaryGroup", temporaryGroup);
        map.put("agvGroup", agvGroup);
        SysConfig config = getByKey("PRODUCT_BONUS");
        if (FlymeUtils.isEmpty(config)) {
            config = new SysConfig();
            config.setConfigKey("PRODUCT_BONUS");
            config.setConfigType("productBonus");
        }
        config.setConfigVal(JsonUtils.beanToJson(map));
        saveOrUpdate(config);
        initBaseConfig();
        return ResultBody.ok("设置成功", true);

    }

    @Override
    public ResultBody saveWeighting(Map params) {
        JSONObject obj = JSON.parseObject(MapUtil.getStr(params, "params"));
        BigDecimal weighting = obj.getBigDecimal("weighting");

        EntityMap map = new EntityMap();
        map.put("weighting", weighting);
        SysConfig config = getByKey("WEIGHTING");
        if (FlymeUtils.isEmpty(config)) {
            config = new SysConfig();
            config.setConfigKey("WEIGHTING");
            config.setConfigType("weighting");
        }
        config.setConfigVal(JsonUtils.beanToJson(map));
        saveOrUpdate(config);
        initBaseConfig();
        return ResultBody.ok("设置成功", true);
    }

    @Override
    public ResultBody saveCommission(Map params) {
        JSONObject obj = JSON.parseObject(MapUtil.getStr(params, "params"));
        BigDecimal product = obj.getBigDecimal("product");
        BigDecimal vip = obj.getBigDecimal("vip");
        BigDecimal vip2vip = obj.getBigDecimal("vip2vip");
        BigDecimal vip2vip2 = obj.getBigDecimal("vip2vip2");
        BigDecimal vip2product = obj.getBigDecimal("vip2product");

        EntityMap map = new EntityMap();
        map.put("product", product);
        map.put("vip2product", vip2product);
        map.put("vip", vip);
        map.put("vip2vip", vip2vip);
        map.put("vip2vip2", vip2vip2);
        SysConfig config = getByKey("COMMISSION");
        if (FlymeUtils.isEmpty(config)) {
            config = new SysConfig();
            config.setConfigKey("COMMISSION");
            config.setConfigType("commission");
        }
        config.setConfigVal(JsonUtils.beanToJson(map));
        saveOrUpdate(config);
        initBaseConfig();
        return ResultBody.ok("设置成功", true);
    }

    @Override
    public ResultBody savePopularize(Map params) {

        SysConfig config = getByKey("POPULARIZE");
        if (FlymeUtils.isEmpty(config)) {
            config = new SysConfig();
            config.setConfigKey("POPULARIZE");
            config.setConfigType("popularize");
        }
        config.setConfigVal(params.get("params").toString());
        saveOrUpdate(config);
        initBaseConfig();
        return ResultBody.ok("设置成功", true);
    }

    @Override
    public ResultBody saveVipBonus(Map params) {
        JSONObject obj = JSON.parseObject(MapUtil.getStr(params, "params"));
        BigDecimal sameGroup = obj.getBigDecimal("sameGroup");
        BigDecimal differentGroup = obj.getBigDecimal("differentGroup");
        BigDecimal temporaryGroup = obj.getBigDecimal("temporaryGroup");
        BigDecimal agvGroup = obj.getBigDecimal("agvGroup");

        EntityMap map = new EntityMap();
        map.put("sameGroup", sameGroup);
        map.put("differentGroup", differentGroup);
        map.put("temporaryGroup", temporaryGroup);
        map.put("agvGroup", agvGroup);
        SysConfig config = getByKey("VIP_BONUS");
        if (FlymeUtils.isEmpty(config)) {
            config = new SysConfig();
            config.setConfigKey("VIP_BONUS");
            config.setConfigType("vipBonus");
        }
        config.setConfigVal(JsonUtils.beanToJson(map));
        saveOrUpdate(config);
        initBaseConfig();
        return ResultBody.ok("设置成功", true);

    }
}
